import unittest


class TestMathFunc(unittest.TestCase):
    '''
    @classmethod:
        setUpClass: 执行测试用例前需要的准备条件
        tearDownClass: 执行测试用例完成后，需要收尾的动作
    '''
    @classmethod
    def setUpClass(cls):
        print("test start")

    def test_abs(self):
        """Test method abs()"""
        self.assertEqual(1, abs(-1))
        self.assertEqual(1, abs(1))

    def test_max(self):
        """Test method max(x1,x2...)"""
        self.assertEqual(2, max(1, 2))

    '''
    skip修饰器：
    unittest.skip(reason) 无条件跳过。
    unittest.skipIf(condition, reason) 当 condition 为 True 时跳过。
    unittest.skipUnless(condition, reason) 当 condition 为 False 时跳过。
    '''

    @unittest.skip("skip test_min()")
    def test_min(self):
        """Test method min(x1,x2...)"""
        self.assertEqual(1, min(1, 2))

    @classmethod
    def tearDownClass(cls):
        print("test end")


# 创建测试套件
# suite = unittest.TestSuite()
loader = unittest.TestLoader()

# 1.13模块编程.12package. 添加部分测试用例
# tests = [TestMathFunc("test_max"), TestMathFunc("test_min")]
# suite.addTests(tests)

# 2. 添加所有测试用例
suite = loader.loadTestsFromTestCase(TestMathFunc)

# runner = unittest.TextTestRunner(verbosity=2) # verbosity=0-2 调整输出
# runner.run(suite)

with open('unittest_report.txt', 'w') as f:
    runner = unittest.TextTestRunner(stream=f, verbosity=2)
    runner.run(suite)
